﻿using BaseUI;
using BLL;
using DevExpress.Web;
using Model;
using System;
using System.Collections;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;

namespace Thksoft.MainAspx
{
    /// <summary>
    /// 导航栏
    /// </summary>
    public partial class Left_Menu : BasePage
    {
        #region 变量、对象定义区

        protected IList lstMenu = new ArrayList();
        protected StringBuilder urlStr;
        protected string _url = string.Empty;
        private Sys_ModuleBLL smb = new Sys_ModuleBLL();
        private Sys_FavoriteBLL sfb = new Sys_FavoriteBLL();

        #endregion 变量、对象定义区

        #region 页面加载

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.CreateGroup();
                this.Favorite2();

                // 设置图片
                ASPxNavBar[] navBars = new ASPxNavBar[] { NavBar1, NavBar2, NavBar3, NavBar4, NavBar5, NavBar6 };
                foreach (ASPxNavBar navbar in navBars)
                {
                    navbar.GroupHeaderStyle.CssClass = "navGp";
                    navbar.GroupHeaderStyle.HoverStyle.CssClass = "navGpHover";
                    navbar.ItemStyle.CssClass = "navMi";
                    navbar.ItemStyle.HoverStyle.CssClass = "navMiHover";

                    navbar.GroupSpacing = Unit.Parse("0px");
                    navbar.Width = Unit.Parse("100%");
                    navbar.EnableAnimation = true;
                    navbar.EncodeHtml = false;
                    navbar.AllowSelectItem = true;
                    navbar.ItemLinkMode = DevExpress.Web.ItemLinkMode.TextOnly;
                }
            }
        }

        #endregion 页面加载

        #region 创建菜单选项卡

        #region 创建菜单组

        private void CreateGroup()
        {
            DataTable dtmg = new DataTable();
            dtmg.Columns.Add("ID", Type.GetType("System.String"));
            dtmg.Columns.Add("NAME", Type.GetType("System.String"));

            Qs_Base_Set model = new Qs_Base_SetBLL().GetModelByName("选项卡");

            if (model != null)
            {
                string[] Card = model.Set_value.Split(';');
                for (int c = 0; c < Card.Length; c++)
                {
                    DataRow newRow = dtmg.NewRow();
                    newRow["ID"] = Card[c].Split(',')[0];
                    newRow["NAME"] = Card[c].Split(',')[1];
                    dtmg.Rows.Add(newRow);
                }
            }

            Session["ModuleGroup"] = dtmg;

            DataTable dt;// = smb.GetModuleByRoleId2(lognUser.USER_ROLEID);
            if (lognUser.USER_ID == 1)
            {
                dt = smb.GetModuleByAll("登录权限查询", lognUser.USER_NAME);
                //超级管理员默认拥有所有功能
                //  var sql = "SELECT T2.MODULE_ID AS D0, T2.MODULE_NAME AS D1, 2 AS D2, T1.MODULE_ID AS D3, T1.MODULE_NAME AS D4, 1 AS D5, T1.MODULE_PARENTID AS D6, '' AS D7, 0 AS D8, T2.MODULE_PATH AS D9 FROM THKQS.SYS_MODULE T1 INNER JOIN THKQS.SYS_MODULE T2 ON T1.MODULE_ID = T2.MODULE_PARENTID WHERE T2.MODULE_STATE = 1 AND T1.MODULE_STATE = 1 AND T1.MODULE_PARENTID = -1 ORDER BY T1.MODULE_PARENTID DESC, T1.MODULE_ORDERNUM1 ASC, T2.MODULE_ORDERNUM2 ASC";
                //  dt = Utility.DbHelperSQL.GetDataTable(sql);
            }
            else
            {
                dt = smb.GetModuleByRoleId2(lognUser.USER_ROLEID);
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //DataRow dr=(dtmg.Select("ID='" + dt.Rows[i]["D6"].ToString()+"'"))[0];
                dt.Rows[i]["D7"] = dtmg.Rows[0]["NAME"].ToString();
            }

            int k = 0;
            var xdt = dt.Copy();

            xdt.Clear();
            {
                /*加入一个默认抽屉盒子（装载个人设置）*/

                #region 装载数据

                #region 注释

                /*
                {
                    var row1 = xdt.NewRow();
                    row1["D0"] = 562;
                    row1["D1"] = "我的待办";
                    row1["D2"] = 2;
                    row1["D3"] = 561;
                    row1["D4"] = "个人设置";
                    row1["D5"] = 1;
                    row1["D6"] = -1;
                    row1["D7"] = "主菜单";
                    row1["D8"] = 0;
                    row1["D9"] = "MyIndexEx.aspx";
                    xdt.Rows.Add(row1);
                }
                {
                    var row1 = xdt.NewRow();
                    row1["D0"] = 563;
                    row1["D1"] = "密码修改";
                    row1["D2"] = 2;
                    row1["D3"] = 561;
                    row1["D4"] = "个人设置";
                    row1["D5"] = 1;
                    row1["D6"] = -1;
                    row1["D7"] = "主菜单";
                    row1["D8"] = 0;
                    row1["D9"] = "BusinessExpansion/Templete/ChangeUserPassword.aspx";
                    xdt.Rows.Add(row1);
                }
                {
                    var row1 = xdt.NewRow();
                    row1["D0"] = 564;
                    row1["D1"] = "处理意见";
                    row1["D2"] = 2;
                    row1["D3"] = 561;
                    row1["D4"] = "个人设置";
                    row1["D5"] = 1;
                    row1["D6"] = -1;
                    row1["D7"] = "主菜单";
                    row1["D8"] = 0;
                    row1["D9"] = "BusinessExpansion/Business/Optinion.aspx";
                    xdt.Rows.Add(row1);
                }
                {
                    var row1 = xdt.NewRow();
                    row1["D0"] = 565;
                    row1["D1"] = "签章管理";
                    row1["D2"] = 2;
                    row1["D3"] = 561;
                    row1["D4"] = "个人设置";
                    row1["D5"] = 1;
                    row1["D6"] = -1;
                    row1["D7"] = "主菜单";
                    row1["D8"] = 0;
                    row1["D9"] = "BusinessExpansion/Templete/ChangeSignaturePassword.aspx";
                    xdt.Rows.Add(row1);
                }
                {
                    var row1 = xdt.NewRow();
                    row1["D0"] = 566;
                    row1["D1"] = "用户信息";
                    row1["D2"] = 2;
                    row1["D3"] = 561;
                    row1["D4"] = "个人设置";
                    row1["D5"] = 1;
                    row1["D6"] = -1;
                    row1["D7"] = "主菜单";
                    row1["D8"] = 0;
                    row1["D9"] = "BusinessExpansion/Business/UserInfo.aspx";
                    xdt.Rows.Add(row1);
                }
                */

                #endregion 注释

                foreach (DataRow item in dt.Rows)
                {
                    var row = xdt.NewRow();
                    row["D0"] = item["D0"];
                    row["D1"] = item["D1"];
                    row["D2"] = item["D2"];
                    row["D3"] = item["D3"];
                    row["D4"] = item["D4"];
                    row["D5"] = item["D5"];
                    row["D6"] = item["D6"];
                    row["D7"] = item["D7"];
                    row["D8"] = item["D8"];
                    row["D9"] = item["D9"];
                    xdt.Rows.Add(row);
                }

                #endregion 装载数据
            }
            while (xdt.Rows.Count != 0)
            {
                ++k;

                IList lst = new ArrayList();
                lst.Add(xdt.Rows[0][7].ToString());  // 菜单名称
                lst.Add(xdt.Rows[0][6].ToString());  //菜单ID
                lstMenu.Add(lst);

                //创建抽屉菜单控件
                this.CreateControls(xdt.Rows[0][6].ToString(), k, xdt);
            }
        }

        #endregion 创建菜单组

        #region 创建抽屉菜单控件

        private void CreateControls(string sys_id, int navindex, DataTable dt)
        {
            DataRow[] dtrows = dt.Select("d6=" + decimal.Parse(sys_id));
            NavBarGroup group = null;

            while (dtrows.Length != 0)
            {
                group = new NavBarGroup();
                group.Name = dtrows[0][3].ToString();
                group.Text = dtrows[0][4].ToString();
                group.Target = "MenuControl";
                group.Expanded = false; // 初始时不展开

                //创建子项
                this.CreateItem(dtrows[0][3].ToString(), group, dt);
                dtrows = dt.Select("d6=" + decimal.Parse(sys_id));

                if (navindex == 1)
                    NavBar1.Groups.Add(group);
                else if (navindex == 2)
                    NavBar2.Groups.Add(group);
                else if (navindex == 3)
                    NavBar3.Groups.Add(group);
                else if (navindex == 4)
                    NavBar4.Groups.Add(group);
                else if (navindex == 5)
                    NavBar5.Groups.Add(group);
                else if (navindex == 6)
                    NavBar6.Groups.Add(group);
            }
        }

        #endregion 创建抽屉菜单控件

        #region 创建菜单项

        private void CreateItem(string parent_id, NavBarGroup group, DataTable dt)
        {
            DataRow[] dtrows = dt.Select("d3=" + decimal.Parse(parent_id));
            NavBarItem items = null;

            for (int row = 0; row < dtrows.Length; row++)
            {
                items = new NavBarItem();
                items.Text = dtrows[row][1].ToString();
                items.Target = "MenuControl";

                // 选项卡模式
                if (dtrows[row][8].ToString() != "1" || dtrows[row][9].ToString().StartsWith("UUID", StringComparison.OrdinalIgnoreCase))
                    items.NavigateUrl = string.Format("MenuControl.aspx?ID={0}", dtrows[row][0].ToString());
                else
                {
                    //窗口模式
                    urlStr = new StringBuilder();
                    _url = dtrows[row][9].ToString();

                    if (!_url.ToLower().StartsWith("http"))
                    {
                        urlStr.Append("../");
                        urlStr.Append(_url);
                        if (!_url.ToLower().EndsWith(".exe"))
                        {
                            urlStr.Append(_url.IndexOf("?") == -1 ? "?" : "&");
                            urlStr.Append("mod_id=").Append(dtrows[row][0].ToString());
                        }
                        items.NavigateUrl = string.Format("javascript:window.open('{0}','','_blank,scrollbars=no,top=0,left=0,status=no,toolbar=no,location=no,menubar=no,resizable=yes,width=' + screen.width + ',height=' + screen.height*0.9+'')", urlStr.ToString());
                    }
                }

                group.Items.Add(items);
                dt.Rows.Remove(dtrows[row]);
            }
        }

        #endregion 创建菜单项

        #endregion 创建菜单选项卡

        #region 收藏夹

        private void Favorite()
        {
            DataTable dtmg = new DataTable();
            dtmg.Columns.Add("ID", Type.GetType("System.String"));
            dtmg.Columns.Add("NAME", Type.GetType("System.String"));

            Qs_Base_Set model = new Qs_Base_SetBLL().GetModelByName("选项卡");
            string[] Card = model.Set_value.Split(';');
            for (int c = 0; c < Card.Length; c++)
            {
                DataRow newRow = dtmg.NewRow();
                newRow["ID"] = Card[c].Split(',')[0];
                newRow["NAME"] = Card[c].Split(',')[1];
                dtmg.Rows.Add(newRow);
            }

            #region 收藏夹

            DataTable dtFavorite = sfb.GetModuleByAll("用户收藏查询", lognUser.USER_ID);//收藏夹
            //收藏夹
            for (int i = 0; i < dtFavorite.Rows.Count; i++)
            {
                dtFavorite.Rows[i]["D5"] = dtmg.Rows[1]["NAME"].ToString();
            }

            var xdt2 = dtFavorite.Copy();
            xdt2.Clear();
            {
                /*加入一个默认抽屉盒子（装载个人设置）*/

                #region 装载数据

                foreach (DataRow item in dtFavorite.Rows)
                {
                    var row = xdt2.NewRow();
                    row["D0"] = item["D0"];
                    row["D1"] = item["D1"];
                    row["D2"] = item["D2"];
                    row["D3"] = item["D3"];
                    row["D4"] = item["D4"];
                    row["D5"] = item["D5"];
                    row["D6"] = item["D6"];
                    xdt2.Rows.Add(row);
                }

                #endregion 装载数据
            }
            while (xdt2.Rows.Count != 0)
            {
                IList lst2 = new ArrayList();
                lst2.Add(xdt2.Rows[0][5].ToString());  // 菜单名称
                lst2.Add(xdt2.Rows[0][4].ToString());  //菜单ID
                lstMenu.Add(lst2);

                //创建子项
                DataRow[] dtrows = xdt2.Select();
                NavBarItem items = null;

                for (int row = 0; row < dtrows.Length; row++)
                {
                    items = new NavBarItem();
                    items.Text = dtrows[row][1].ToString();
                    items.Target = "MenuControl";

                    // 选项卡模式
                    if (dtrows[row][6].ToString() != "1" || dtrows[row][2].ToString().StartsWith("UUID", StringComparison.OrdinalIgnoreCase))
                        items.NavigateUrl = string.Format("MenuControl.aspx?ID={0}", dtrows[row][0].ToString());
                    else
                    {
                        //窗口模式
                        urlStr = new StringBuilder();
                        _url = dtrows[row][2].ToString();

                        if (!_url.ToLower().StartsWith("http"))
                        {
                            urlStr.Append("../");
                            urlStr.Append(_url);
                            if (!_url.ToLower().EndsWith(".exe"))
                            {
                                urlStr.Append(_url.IndexOf("?") == -1 ? "?" : "&");
                                urlStr.Append("mod_id=").Append(dtrows[row][0].ToString());
                            }
                            items.NavigateUrl = string.Format("javascript:window.open('{0}','','_blank,scrollbars=no,top=0,left=0,status=no,toolbar=no,location=no,menubar=no,resizable=yes,width=' + screen.width + ',height=' + screen.height*0.9+'')", urlStr.ToString());
                        }
                    }

                    this.Items.Add(items, items);

                    dtFavorite.Rows.Remove(dtrows[row]);
                }

                dtrows = dtFavorite.Select();
            }

            #endregion 收藏夹
        }

        private void Favorite2()
        {
            string InerHtmls = "";
            //查询待办信息
            DataTable temp = sfb.GetDataAll(CurrentUserID);

            #region 收藏

            if (temp != null && temp.Rows.Count > 0)
            {
                for (int i = 0; i < temp.Rows.Count; i++)
                {
                    InerHtmls += "<a href='#' onclick=\"top.ICard.Open('" + "/" + temp.Rows[i]["C_FAVORITE_URL"].ToString() + "','" + temp.Rows[i]["C_FAVORITE_NAME"].ToString() + "');\" style='text-decoration: none; cursor: pointer;'>";
                    InerHtmls += "<div class='leftmenuzi'>";//dxnb-gr dxnb-t;dxnb-header navGp;dxWeb_nbCollapse_DevEx dxnb-btn;dxnb-ghtext dx-vam;dxnb-item navMi dxnb-link;
                    InerHtmls += "<img alt='' src='../Images/ico_07.gif' align='middle' style='*margin-top: 8px; vertical-align: -2px;'/>";
                    InerHtmls += "<span style='text-align: center; margin-left: auto; margin-right: auto; font-size: 13px; vertical-align: middle; color: #054882; line-height: 25px;'>" + temp.Rows[i]["C_FAVORITE_NAME"].ToString() + "</span>";
                    InerHtmls += "</div></a>";
                }
            }

            #endregion 收藏

            this.Div1.InnerHtml = InerHtmls;
        }

        #endregion 收藏夹
    }
}